package com.thunder.kocho.loki;

import android.animation.Animator;
import android.animation.Keyframe;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.animation.ValueAnimator;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.animation.LinearInterpolator;

import com.thunder.kocho.loki.shape.CircleLoki;

import java.util.ArrayList;

/**
 * @author Hilex
 * @date 2021-3-8
 */
public class ScaleCircle extends LokiGroup{

    @Override
    protected ArrayList<Loki> onCreateChild() {
        ArrayList<Loki> lokis=new ArrayList<>();
        for(int i=0;i<2;i++){
            Loki loki=new ScaleCircleLoki(i);
            loki.setAnimationDelays(0);
            lokis.add(loki);
        }
        return lokis;
    }

    @Override
    protected void onBoundsChange(Rect bounds) {
        super.onBoundsChange(bounds);
        bounds=clipSquare(bounds);
        for(Loki loki:mLokis){
            if(loki instanceof ScaleCircleLoki){
                ((ScaleCircleLoki) loki).setRadius(bounds.width()/6);
            }
            loki.setLokiBoundsAndPivot(bounds);
        }

    }

    @Override
    protected void onChildCreated(ArrayList<Loki> lokis) {

    }

    private static class ScaleCircleLoki extends CircleLoki {

        private final int index;

        public ScaleCircleLoki(int index){
            this.index=index;
            Paint paint=new Paint(Paint.ANTI_ALIAS_FLAG);
            paint.setStyle(Paint.Style.FILL);
            if(index==0){
                ColorMatrix colorMatrix = new ColorMatrix(new float[]{
                        1.2f, 0, 0, 0, 0,
                        0, 1.2f, 0, 0, 0,
                        0, 0, 1.2f, 0, 0,
                        0, 0, 0, 0.5f, 0
                });
                paint.setColorFilter(new ColorMatrixColorFilter(colorMatrix));
            }
            mPaint=paint;
        }

        @Override
        protected Animator onCreateAnimator() {
            Keyframe keyframe=Keyframe.ofFloat(0,1);
            Keyframe keyframe1=Keyframe.ofFloat(0.35f,1.75f);
            Keyframe keyframe2=Keyframe.ofFloat(0.7f,1);
            Keyframe keyframe3=Keyframe.ofFloat(0.85f,0);
            Keyframe keyframe4=Keyframe.ofFloat(1,1);

            Keyframe keyframe5=Keyframe.ofFloat(0.35f,0);
            Keyframe keyframe6= Keyframe.ofFloat(0.7f,1);
            Keyframe keyframe7= Keyframe.ofFloat(0.85f,1.5f);
            Keyframe keyframe8= Keyframe.ofFloat(1,1);
            PropertyValuesHolder holder;
            if(index==0){
                holder=PropertyValuesHolder.ofKeyframe("Scale",keyframe,keyframe1,keyframe2,keyframe3,keyframe4);
            }else{
                holder=PropertyValuesHolder.ofKeyframe("Scale",keyframe,keyframe5,keyframe6,keyframe7,keyframe8);
            }
            ObjectAnimator animator=ObjectAnimator.ofPropertyValuesHolder(this,holder);
            animator.setInterpolator(new LinearInterpolator());
            animator.setRepeatCount(ValueAnimator.INFINITE);
            animator.setDuration(2000);
            return animator;
        }

    }

}
